package com.bawei.demo_mvp.http;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author yaotianxue
 * @description
 * @date 2025/5/12
 */
public class RetrofitManager {
    private RetrofitManager(){}
    private volatile static RetrofitManager retrofitManager;//volatile关键字 原子性
    public static RetrofitManager getInstance(){
        if (retrofitManager == null){
            synchronized (RetrofitManager.class){
                if(retrofitManager == null){
                    retrofitManager = new RetrofitManager();
                }
            }
        }
        return retrofitManager;
    }
    private Retrofit mRetrofit;
    public Retrofit getRetrofit() {
        if (mRetrofit == null){
            createRetrofit();
        }
        return mRetrofit;
    }
    //创建Retrofit
    private void createRetrofit() {
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
        mRetrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl("http://10.161.9.80:7012")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

    }
}
